//
//  CVCellOfBelowReadArticle.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfBelowReadArticle.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "TVCellOfReadAriticle.h"
#import "CycleBannerOfReadAcrticle.h"
#import "ReadAritcleTVCellRootClass.h"
#import "ReadAritcleTVCellData.h"
#import "ReadAritcleNeedTVCellData.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
// web
#import "VCOfReadWeb.h"
#import "VCOfBannersWeb.h"
#import "VCOfReadArticle.h"
#import "MJRefresh.h"
@interface CVCellOfBelowReadArticle()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) ReadAritcleTVCellRootClass *readAritcleTVCellRootClass;
@property (nonatomic, retain) BaseTableView *readArticleOfTableView;
@property (nonatomic, strong) NSMutableArray<ReadAritcleNeedTVCellData *> *mArrayOfData;
@end
@implementation CVCellOfBelowReadArticle
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self handleDat];
        [self createTableView];
    }
    return self;
}
#pragma mark - 数据解析
- (void)handleDat{
    // 下面 TableViewCell 数据的处理
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=1&size=10&tag_id=0&tag_type=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=5bc3ae2b26cfded7769af72204cdc105&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _readAritcleTVCellRootClass = [ReadAritcleTVCellRootClass modelDic:result];
        _mArrayOfData = _readAritcleTVCellRootClass.data.data.mutableCopy;
        [self.readArticleOfTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 TableView
- (void)createTableView{
    self.readArticleOfTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.contentView addSubview:self.readArticleOfTableView];
    self.readArticleOfTableView.delegate = self;
    self.readArticleOfTableView.dataSource = self;
    [self.readArticleOfTableView registerNib:[UINib nibWithNibName:@"TVCellOfReadAriticle" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfReadAriticle"];
    CycleBannerOfReadAcrticle *cycleBannerOfReadAcrticle =[[CycleBannerOfReadAcrticle alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3)];
    self.readArticleOfTableView.tableHeaderView = cycleBannerOfReadAcrticle;
    // 上拉刷新
    self.readArticleOfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 下拉加载
    self.readArticleOfTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
};
#pragma mark -上拉刷新
- (void)loadNewData{
    [self handleDat];
    [self.readArticleOfTableView.mj_header endRefreshing];
}
#pragma mark - 下拉加载
- (void)loadMoreData{
    static int i = 2;
    [ZWYNetTool GET:[NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/get-article-bytag?page=%d&size=10&tag_id=0&tag_type=0&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=5bc3ae2b26cfded7769af72204cdc105&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93", i] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _readAritcleTVCellRootClass = [ReadAritcleTVCellRootClass modelDic:result];
        for (ReadAritcleNeedTVCellData *readAritcleNeedTVCellData in _readAritcleTVCellRootClass.data.data) {
            [_mArrayOfData addObject:readAritcleNeedTVCellData];
        }
        [self.readArticleOfTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    i++;
    [self.readArticleOfTableView.mj_footer endRefreshing];
}
#pragma mark - tableView 代理方法 row 行数的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mArrayOfData.count;
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfReadAriticle *cellOfReadAriticle = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfReadAriticle"];
    [cellOfReadAriticle.myImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfData[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"0"]];
    cellOfReadAriticle.titleLabel.text = _mArrayOfData[indexPath.row].title;
    cellOfReadAriticle.authorLabel.text = _mArrayOfData[indexPath.row].author;
    cellOfReadAriticle.viewnumLabel.text = _mArrayOfData[indexPath.row].viewnum;
    return cellOfReadAriticle;
}
#pragma mark - 返回每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 6;
}
#pragma mark - tableView 点击 实现方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfReadWeb *vcOfReadWeb = [[VCOfReadWeb alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfReadWeb];
    NSLog(@"%@", self.nextResponder.nextResponder.nextResponder);
    VCOfReadArticle *vcOfReadArticle = (VCOfReadArticle *)self.nextResponder.nextResponder.nextResponder;
    vcOfReadWeb.stringURL = [NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/article-detail-newpage/%@?user_id=1004358517&_version=4.0", _readAritcleTVCellRootClass.data.data[indexPath.row].ID];
    vcOfReadWeb.myID = _readAritcleTVCellRootClass.data.data[indexPath.row].ID;
    NSLog(@"------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%@", _readAritcleTVCellRootClass.data.data[indexPath.row].ID);
    [vcOfReadArticle presentViewController:navi animated:YES completion:^{
    
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
